/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138;

import com.sun.star.awt.*;
import com.sun.star.lang.EventObject;
import com.sun.star.awt.XActionListener;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.lang.XComponent;
import cz.muni.fi.pb138.exceptions.XmlWriteException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Marek
 */
public class SaveSettingsActionListenerImpl implements com.sun.star.awt.XActionListener {

    private int _nCounts = 0;
    private XControlContainer _xControlCont;
    private Manager _mgr;
    private String _usernameTextFieldName = "UsernameTextField";
    private String _passwordTextFieldName = "PasswordTextField";
    private String _dictionaryFieldName = "DictionaryField";
    private XDialog _dialog;
    /**
     * Action Listener Constructor for Save Settings button
     * @param xControlCont
     * @param mgr Manager class instance
     * @param usernameField Internal name for username field
     * @param passField Internal name for password field
     * @param dictField Internal name for select dictionary field
     * @param dialog parent dialog
     */
    public SaveSettingsActionListenerImpl(XControlContainer xControlCont, Manager mgr, String usernameField, String passField, String dictField, XDialog dialog) {
        _xControlCont = xControlCont;
        this._mgr = mgr;
        this._usernameTextFieldName = usernameField;
        this._passwordTextFieldName = passField;
        this._dictionaryFieldName = dictField;
        this._dialog = dialog;
        System.out.println("Creating listener");
    }

    // XEventListener
    public void disposing(EventObject eventObject) {
        _xControlCont = null;
    }

    // XActionListener
    /**
     * Method called when button is clicked. Save given data and disposes dialog
     * @param actionEvent
     */
    public void actionPerformed(ActionEvent actionEvent) {
        Object usernameField = _xControlCont.getControl(_usernameTextFieldName);
        XTextComponent xUsername = (XTextComponent) UnoRuntime.queryInterface(
                XTextComponent.class, usernameField);
        String usernameValue = xUsername.getText();

        Object passField = _xControlCont.getControl(_passwordTextFieldName);
        XTextComponent xPass = (XTextComponent) UnoRuntime.queryInterface(
                XTextComponent.class, passField);
        String passValue = xPass.getText();


        Object dictionaryField = _xControlCont.getControl(_dictionaryFieldName);
        XListBox xDictionary = (XListBox) UnoRuntime.queryInterface(
                XListBox.class, dictionaryField);
        String dictValue = xDictionary.getSelectedItem();

        System.out.println(usernameValue +", "+ passValue +", "+_mgr.translateString2Dictionary(dictValue));
        try {
            //TODO:alert
            _mgr.changeSettings(usernameValue, passValue, _mgr.translateString2Dictionary(dictValue));
        } catch (XmlWriteException ex) {
            Logger.getLogger(SaveSettingsActionListenerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        // dispose the dialog
        
        _dialog.endExecute();
        
    }
}
